#include "Objective.hpp"
#include "ConcurrentAlloc.h"
#include "PageMap.h"
void Alloc1()
{
	for (size_t i = 0; i < 5; ++i)
	{
		void* ptr = ConcurrentAlloc(6);
		cout << i <<":" << ptr << endl;
	}
}

void Alloc2()
{
	for (size_t i = 0; i < 5; ++i)
	{
		void* ptr = ConcurrentAlloc(7);
		cout << i << ":" << ptr << endl;
	}
}
//
void TLSTest()
{
	std::thread t1(Alloc1);
	t1.join();

	std::thread t2(Alloc2);
	t2.join();
}
//
//void TestConcurrentAlloc1()
//{
//	void* p1 = ConcurrentAlloc(6);
//	void* p2 = ConcurrentAlloc(8);
//	void* p3 = ConcurrentAlloc(1);
//	void* p4 = ConcurrentAlloc(7);
//	void* p5 = ConcurrentAlloc(8);
//
//	cout << p1 << endl;
//	cout << p2 << endl;
//	cout << p3 << endl;
//	cout << p4 << endl;
//	cout << p5 << endl;
//}
//
void TestConcurrentAlloc2()
{
	void* p1[10];
	for (size_t i = 0; i < 10; ++i)
	{
		cout << i << endl;
		p1[i] = ConcurrentAlloc(10);
		//cout << p1 << endl;
	}

	/*void* p2 = ConcurrentAlloc(8);
	cout << p2 << endl;*/
	/*for (int i = 0; i < 1024; i++)
	{
		ConcurrentFree(p1[i], 10);
	}*/
	
}

void MultiThreadAlloc1()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 7; ++i)
	{
		void* ptr = ConcurrentAlloc(1);
		v.push_back(ptr);
	}

	for(auto e:v)
		ConcurrentFree(e);
	
}

void MultiThreadAlloc2()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 7; ++i)
	{
		void* ptr = ConcurrentAlloc(16);
		v.push_back(ptr);
	}

	for (auto e : v)
	{
		ConcurrentFree(e);
	}
}

void TestMultiThread()
{
	//MultiThreadAlloc1();
	std::thread t1(MultiThreadAlloc1);
	std::thread t2(MultiThreadAlloc2);

	t1.join();
	t2.join();
}

//void BigAlloc()
//{
//	void* p1 = ConcurrentAlloc(257 * 1024);
//	ConcurrentFree(p1);
//
//	void* p2 = ConcurrentAlloc(12900 * 8 * 1024);
//	ConcurrentFree(p2);
//}


//int main()
//{
//	//TestObjectPool();
//	//TLSTest();
//
//	//TestConcurrentAlloc2();
//
//	//TLSTest();
//
//	TestMultiThread();
//	//MultiThreadAlloc1();
//	//BigAlloc();
//	return 0;
//}